﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CTBHDataAccess
    {
        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceLockedBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return num;
        }

        public int CheckInvoicePost(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Check", null) };
            values[4].Direction = ParameterDirection.Output;
            values[4].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoicePostedBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[4].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return num;
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckInvoiceStatusBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[3].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return num;
        }

        private bool CheckOverFlow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_Signal, object ps_InvoiceOrder, object pi_Posted)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Signal", pb_Signal), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Check", null) };
            values[3].Direction = ParameterDirection.Output;
            values[3].SqlDbType = SqlDbType.Bit;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_RefTable_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = (bool) values[3].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public object Cong_So_Ct(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            object obj2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ma_Ct", ps_InvoiceCode), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_So_Ct", ps_So_Ct) };
            values[3].Direction = ParameterDirection.InputOutput;
            values[3].SqlDbType = SqlDbType.NVarChar;
            values[3].Size = 20;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "Cong_So_CtBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                obj2 = values[3].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return obj2;
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", po_KeyCode), new SqlParameter("p_UserName", ps_UserName), new SqlParameter() };
            values[2].Direction = ParameterDirection.ReturnValue;
            values[2].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                flag = ((int) values[2].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public bool DeleteDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDetailDataSet(string ps_InvoiceOrder)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH0_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataTable GetDetailSales(object ps_MasterKey, object ps_ParentKey)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_MasterKey), new SqlParameter("p_Stt_D", ps_ParentKey) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH0_CtXTL_SL_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataRow GetMasterDataRow(string ps_InvoiceOrder)
        {
            DataRow row;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_One_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                if (dataTable.Rows.Count == 0)
                {
                    dataTable.Rows.Add(dataTable.NewRow());
                    dataTable.AcceptChanges();
                }
                row = dataTable.Rows[0];
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return row;
        }

        public DataTable GetMasterSales(bool pb_FirstTime, string ps_Subsidiary, object ps_ParentKey, object pd_Ngay_Ct, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_Ma_Dt)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Check_First", pb_FirstTime), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Stt", ps_ParentKey), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_Ngay_Ct1", pd_Ngay_Ct1), new SqlParameter("p_Ngay_Ct2", pd_Ngay_Ct2), new SqlParameter("p_Ma_Dt", ps_Ma_Dt) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_CtXTL_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public DataTable GetMasterTable(string ps_Subsidiary, int pi_DaysFilter, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_So_Ct1, object ps_So_Ct2, object ps_Ma_Ct, object ps_Ma_Tte, object ps_Tk, object ps_Tk_Du, object ps_Ma_Dt, object pi_Tien1, object pi_Tien2, object ps_Ma_Km, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Nx, object ps_Ma_Kho, object ps_Ma_Vt, object ps_Dien_Giai, object ps_Ma_Loai1, object ps_Ma_Loai2, object ps_Ma_Loai3, int ii_FilterByUser, string ps_UserName, object ps_Notes, int pi_Posted, int pi_Locked)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Ma_DvCs", ps_Subsidiary), new SqlParameter("p_Ngay_Loc", pi_DaysFilter), new SqlParameter("p_Ngay1", pd_Ngay_Ct1), new SqlParameter("p_Ngay2", pd_Ngay_Ct2), new SqlParameter("p_So_Ct1", ps_So_Ct1), new SqlParameter("p_So_Ct2", ps_So_Ct2), new SqlParameter("p_Ma_Ct", ps_Ma_Ct), new SqlParameter("p_Ma_Tte", ps_Ma_Tte), new SqlParameter("p_Tk", ps_Tk), new SqlParameter("p_Tk_Du", ps_Tk_Du), new SqlParameter("p_Ma_Dt", ps_Ma_Dt), new SqlParameter("p_Tien1", pi_Tien1), new SqlParameter("p_Tien2", pi_Tien2), new SqlParameter("p_Ma_Km", ps_Ma_Km), new SqlParameter("p_Ma_Bp", ps_Ma_Bp), new SqlParameter("p_Ma_HD", ps_Ma_Hd), 
                new SqlParameter("p_Ma_Nx", ps_Ma_Nx), new SqlParameter("p_Ma_Kho", ps_Ma_Kho), new SqlParameter("p_Ma_Vt", ps_Ma_Vt), new SqlParameter("p_Dien_Giai", ps_Dien_Giai), new SqlParameter("p_Ma_Loai1", ps_Ma_Loai1), new SqlParameter("p_Ma_Loai2", ps_Ma_Loai2), new SqlParameter("p_Ma_Loai3", ps_Ma_Loai3), new SqlParameter("p_FilterByUser", ii_FilterByUser), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Notes", ps_Notes), new SqlParameter("p_Posted", pi_Posted), new SqlParameter("p_Locked", pi_Locked)
             };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Locked", pb_Locked) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdateLockedBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Moi_Sua", pb_CheckNewRow), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Ma_Ct", ps_InvoiceCode), new SqlParameter("p_Ngay_Ct", pd_Ngay_Ct), new SqlParameter("p_So_Ct", ps_So_Ct), new SqlParameter("p_Check", null) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Bit;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CheckSoCtBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                flag = (bool) values[6].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public bool Post(string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            bool flag2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder), new SqlParameter("p_Posted", pi_Posted) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlTransaction transaction = eZConnection.BeginTransaction();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "UpdatePostedBH";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Transaction = transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                bool flag = this.CheckOverFlow(eZConnection, transaction, false, ps_InvoiceOrder, pi_Posted);
                if ((((int) pi_Posted) != 0) && !flag)
                {
                    transaction.Rollback();
                    AppDataAccess.CloseConnection(eZConnection);
                    return false;
                }
                transaction.Commit();
                AppDataAccess.CloseConnection(eZConnection);
                flag2 = true;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag2;
        }

        private void PostSoCai(SqlConnection po_Connection, SqlTransaction po_Transaction, object ps_InvoiceOrder, object pi_Posted)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.CommandText = "CtBH_SoCaiVT_Save";
                command.ExecuteNonQuery();
                command.CommandText = "CtBH_SoCaiCN_Save";
                command.ExecuteNonQuery();
                if (((int) pi_Posted) != 0)
                {
                    command.CommandText = "SoCaiTt_Calculate";
                    command.ExecuteNonQuery();
                }
                command.CommandText = "CtBH_SoCaiKT_Save";
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, DataRow po_MasterDataRow, DataTable po_DetailTable, ref bool pb_CheckOverFlow)
        {
            bool flag = false;
            foreach (DataRow row in po_DetailTable.Rows)
            {
                if (((row.RowState != DataRowState.Deleted) && !string.IsNullOrEmpty(row["Ma_Vt"].ToString())) && (((((decimal) row["So_Luong9"]) != 0M) || (((decimal) row["Tien_Nt9"]) != 0M)) || (((decimal) row["Tien9"]) != 0M)))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                this.Delete(ps_UserName, po_MasterDataRow["Stt"]);
                return false;
            }
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlTransaction transaction = eZConnection.BeginTransaction();
            if (!pb_NewChecked && pb_InvoiceHistory)
            {
                this.SaveHistory(eZConnection, transaction, ps_UserName, po_MasterDataRow["Stt"]);
            }
            flag = true;
            foreach (DataRow row2 in po_DetailTable.Rows)
            {
                row2.EndEdit();
                if (((row2.RowState != DataRowState.Deleted) && !string.IsNullOrEmpty(row2["Ma_Vt"].ToString())) && (((((decimal) row2["So_Luong9"]) != 0M) || (((decimal) row2["Tien_Nt9"]) != 0M)) || (((decimal) row2["Tien9"]) != 0M)))
                {
                    if (((row2.RowState != DataRowState.Added) && (row2.RowState != DataRowState.Modified)) || (this.SaveDetailDataRow(eZConnection, transaction, po_MasterDataRow["Stt"], row2) == 1))
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row2.RowState == DataRowState.Deleted)
                {
                    this.DeleteDetailDataRow(eZConnection, transaction, row2["Stt0", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteDetailDataRow(eZConnection, transaction, row2["Stt0"]);
                }
            }
            if (flag && (this.SaveMasterDataRow(eZConnection, transaction, pb_NewChecked, ps_Subsidiary, po_MasterDataRow) == 1))
            {
                this.PostSoCai(eZConnection, transaction, po_MasterDataRow["Stt"], po_MasterDataRow["Posted"]);
                if ((((int) po_MasterDataRow["Posted"]) != 0) && !this.CheckOverFlow(eZConnection, transaction, true, po_MasterDataRow["Stt"], po_MasterDataRow["Posted"]))
                {
                    pb_CheckOverFlow = false;
                    transaction.Rollback();
                    AppDataAccess.CloseConnection(eZConnection);
                    return false;
                }
                transaction.Commit();
                AppDataAccess.CloseConnection(eZConnection);
                po_DetailTable.AcceptChanges();
                po_MasterDataRow.Table.AcceptChanges();
                return true;
            }
            transaction.Rollback();
            AppDataAccess.CloseConnection(eZConnection);
            return false;
        }

        public int SaveDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Stt_Nv", row["Stt_Nv"]), new SqlParameter("p_Ma_Vt", row["Ma_Vt"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ma_Hd", row["Ma_Hd"]), new SqlParameter("p_Ma_Km", row["Ma_Km"]), new SqlParameter("p_Ma_Kho", row["Ma_Kho"]), new SqlParameter("p_Dvt", row["Dvt"]), new SqlParameter("p_He_So9", row["He_So9"]), new SqlParameter("p_So_Luong9", row["So_Luong9"]), new SqlParameter("p_Gia_Nt9", row["Gia_Nt9"]), new SqlParameter("p_Gia9", row["Gia9"]), new SqlParameter("p_Tien_Nt9", row["Tien_Nt9"]), new SqlParameter("p_Tien9", row["Tien9"]), new SqlParameter("p_So_Luong", row["So_Luong"]), 
                new SqlParameter("p_Gia_Tb_Tt", row["Gia_Tb_Tt"]), new SqlParameter("p_Gia_Nt", row["Gia_Nt"]), new SqlParameter("p_Gia", row["Gia"]), new SqlParameter("p_Tien_Nt", row["Tien_Nt"]), new SqlParameter("p_Tien", row["Tien"]), new SqlParameter("p_Gia_Nt2", row["Gia_Nt2"]), new SqlParameter("p_Gia2", row["Gia2"]), new SqlParameter("p_Tien_Nt2", row["Tien_Nt2"]), new SqlParameter("p_Tien2", row["Tien2"]), new SqlParameter("p_Thue_GTGT", row["Thue_GTGT"]), new SqlParameter("p_Tien_Nt3", row["Tien_Nt3"]), new SqlParameter("p_Tien3", row["Tien3"]), new SqlParameter("p_Thue_XK", row["Thue_XK"]), new SqlParameter("p_Tien_Nt5", row["Tien_Nt5"]), new SqlParameter("p_Tien5", row["Tien5"]), new SqlParameter("p_Thue_TTDB", row["Thue_TTDB"]), 
                new SqlParameter("p_Tien_Nt8", row["Tien_Nt8"]), new SqlParameter("p_Tien8", row["Tien8"]), new SqlParameter("p_Chiet_Khau", row["Chiet_Khau"]), new SqlParameter("p_Tien_Nt4", row["Tien_Nt4"]), new SqlParameter("p_Tien4", row["Tien4"]), new SqlParameter("p_Tien_Nt7", row["Tien_Nt7"]), new SqlParameter("p_Tien7", row["Tien7"]), new SqlParameter("p_Tien_Nt6", row["Tien_Nt6"]), new SqlParameter("p_Tien6", row["Tien6"]), new SqlParameter("p_Tk_No", row["Tk_No"]), new SqlParameter("p_Tk_Co", row["Tk_Co"]), new SqlParameter("p_Tk_No2", row["Tk_No2"]), new SqlParameter("p_Tk_Co2", row["Tk_Co2"]), new SqlParameter("p_Tk_No3", row["Tk_No3"]), new SqlParameter("p_Tk_Co3", row["Tk_Co3"]), new SqlParameter("p_Tk_No5", row["Tk_No5"]), 
                new SqlParameter("p_Tk_Co5", row["Tk_Co5"]), new SqlParameter("p_Tk_No8", row["Tk_No8"]), new SqlParameter("p_Tk_Co8", row["Tk_Co8"]), new SqlParameter("p_Tk_No4", row["Tk_No4"]), new SqlParameter("p_Tk_Co4", row["Tk_Co4"]), new SqlParameter("p_Tk_No6", row["Tk_No6"]), new SqlParameter("p_Tk_Co6", row["Tk_Co6"]), new SqlParameter("p_Lo_Hang", row["Lo_Hang"]), new SqlParameter("p_Han_Dung", row["Han_Dung"]), new SqlParameter("p_H_Luong", row["H_Luong"]), new SqlParameter("p_Do_Am_PT", row["Do_Am_PT"]), new SqlParameter("p_Do_Con_PT", row["Do_Con_PT"]), new SqlParameter("p_Stt0_D", row["Stt0_D"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x3d].Direction = ParameterDirection.Output;
            values[0x3d].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH0_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[0x3d].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        private void SaveHistory(SqlConnection po_Connection, SqlTransaction po_Transaction, string ps_UserName, object ps_InvoiceOrder)
        {
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Save_Delete", 1), new SqlParameter("p_UserName", ps_UserName), new SqlParameter("p_Stt", ps_InvoiceOrder) };
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_Log_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
        }

        public int SaveMasterDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Dvcs", ps_Subsidiary), new SqlParameter("p_Ma_Ct", row["Ma_Ct"]), new SqlParameter("p_Ngay_Ps", row["Ngay_Ps"]), new SqlParameter("p_So_Seri", row["So_Seri"]), new SqlParameter("p_Ngay_Ct", row["Ngay_Ct"]), new SqlParameter("p_So_Ct", row["So_Ct"]), new SqlParameter("p_Ma_Bp", row["Ma_Bp"]), new SqlParameter("p_Ma_Hd", row["Ma_Hd"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Ong_Ba", row["Ong_Ba"]), new SqlParameter("p_Dia_Chi", row["Dia_Chi"]), new SqlParameter("p_Dien_Giai", row["Dien_Giai"]), new SqlParameter("p_Desc_EN", row["Desc_EN"]), new SqlParameter("p_Ma_Tte", row["Ma_Tte"]), 
                new SqlParameter("p_Ty_Gia", row["Ty_Gia"]), new SqlParameter("p_Ty_Gia_Ht", row["Ty_Gia_Ht"]), new SqlParameter("p_Ma_Km", row["Ma_Km"]), new SqlParameter("p_Gia_Thue", row["Gia_Thue"]), new SqlParameter("p_Thue_GTGT", row["Thue_GTGT"]), new SqlParameter("p_Ma_Nx", row["Ma_Nx"]), new SqlParameter("p_PostedGL", row["PostedGL"]), new SqlParameter("p_Han_Tt", row["Han_Tt"]), new SqlParameter("p_TTien_Nt0", row["TTien_Nt0"]), new SqlParameter("p_TTien0", row["TTien0"]), new SqlParameter("p_TTien_Nt", row["TTien_Nt"]), new SqlParameter("p_TTien", row["TTien"]), new SqlParameter("p_TTien_Nt2", row["TTien_Nt2"]), new SqlParameter("p_TTien2", row["TTien2"]), new SqlParameter("p_TTien_Nt3", row["TTien_Nt3"]), new SqlParameter("p_TTien3", row["TTien3"]), 
                new SqlParameter("p_TTien_Nt5", row["TTien_Nt5"]), new SqlParameter("p_TTien5", row["TTien5"]), new SqlParameter("p_TTien_Nt8", row["TTien_Nt8"]), new SqlParameter("p_TTien8", row["TTien8"]), new SqlParameter("p_Loai_Ck", row["Loai_Ck"]), new SqlParameter("p_TTien_Nt4", row["TTien_Nt4"]), new SqlParameter("p_TTien4", row["TTien4"]), new SqlParameter("p_TTien_Nt6", row["TTien_Nt6"]), new SqlParameter("p_TTien6", row["TTien6"]), new SqlParameter("p_TTien_VAT0", row["TTien_VAT0"]), new SqlParameter("p_Tien_Cash", row["Tien_Cash"]), new SqlParameter("p_So_Seri0", row["So_Seri0"]), new SqlParameter("p_Ngay_Ct0", row["Ngay_Ct0"]), new SqlParameter("p_So_Ct0", row["So_Ct0"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), 
                new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_UserName", row["UserName"]), new SqlParameter("p_Posted", row["Posted"]), new SqlParameter("p_Locked", row["Locked"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[0x34].Direction = ParameterDirection.Output;
            values[0x34].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "CtBH_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[0x34].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

